//
// Created by TNJ on 2021/6/13.
//

#include <iostream>
#include "sort.h"

//从小到大排序
template<typename T>
void Sort<T>::insert_sort(T *arr, int size){
    if(arr == nullptr || size <= 0){
        return;
    }
    int i = 1;
    int j = 0;
    while(i < size){
        T data = arr[i];
        j = i - 1;
        while(j >= 0 && arr[j] > data){
            arr[j + 1] = arr[j];
            j--;
        }
        if(j < 0){
            j = 0;
        }
        arr[j] = data;
        i++;
    }
}

//冒泡排序
template<typename T>
void Sort<T>::bubble_sort(T *arr, int size) {
    if(arr == nullptr || size <= 0){
        return;
    }
    T temp;
    for(int i = 0; i < size; i++){
        for(int j = 0; j < size - i; j++){
            if(arr[j] > arr[j + 1]){
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

//交换排序
template<typename T>
void Sort<T>::swap_sort(T *arr, int size) {
    if(arr == nullptr || size <= 0){
        return;
    }

    T temp;
    for(int i = 0; i < size - 1; i++){
        for(int j = i + 1; j < size; j++){
            if(arr[i] > arr[j]){
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}